package com.link.play.fit.ai.ui.bmi

import android.annotation.SuppressLint
import android.content.Intent
import android.content.res.ColorStateList
import android.graphics.Paint
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.LayerDrawable
import android.net.Uri
import android.os.Bundle
import androidx.activity.addCallback
import androidx.lifecycle.lifecycleScope
import com.google.firebase.firestore.Filter
import com.link.play.fit.ai.R
import com.link.play.fit.ai.base.BaseActivity
import com.link.play.fit.ai.base.viewBind
import com.link.play.fit.ai.bean.LogWeightBean
import com.link.play.fit.ai.bean.UserGuide
import com.link.play.fit.ai.databinding.AcBmiBinding
import com.link.play.fit.ai.ext.setThrottleListener
import com.link.play.fit.ai.store.CloudStore
import com.link.play.fit.ai.store.CloudStore.DOC_LOG_WEIGHT
import com.link.play.fit.ai.store.CloudStore.DOC_USER_GUIDE
import com.link.play.fit.ai.utils.BmiUtils
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import androidx.core.net.toUri

/**
 *Create by SleepDog on 2025-04-02
 */
class BmiAc : BaseActivity<AcBmiBinding>(AcBmiBinding::inflate) {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initView()
    }

    @SuppressLint("ClickableViewAccessibility")
    private fun initView() {
        viewBind {
            // 创建渐变色的Drawable
            val gradientDrawable = GradientDrawable(
                GradientDrawable.Orientation.LEFT_RIGHT,
                intArrayOf(
                    resources.getColor(R.color.color_007BFF, null),
                    resources.getColor(R.color.color_34C85A, null),
                    resources.getColor(R.color.color_FFD60A, null),
                    resources.getColor(R.color.color_FF3B2F, null)
                )
            )
            gradientDrawable.gradientType = GradientDrawable.LINEAR_GRADIENT
            gradientDrawable.shape = GradientDrawable.RECTANGLE
            gradientDrawable.cornerRadius = 9f // 设置圆角

            // 创建LayerDrawable，用于设置进度条的背景
            val layerDrawable = LayerDrawable(arrayOf(gradientDrawable))
            sbBmi.progressDrawable = layerDrawable
            sbBmi.setOnTouchListener { _, _ -> true }
            tvSource.paint.flags = Paint.UNDERLINE_TEXT_FLAG
            onBackPressedDispatcher.addCallback {
                finishAfterTransition()
            }

            ivBack.setOnClickListener {
                onBackPressedDispatcher.onBackPressed()
            }
            tvSource.setThrottleListener {
                runCatching {
                    val intent = Intent(
                        Intent.ACTION_VIEW,
                        "https://www.cdc.gov/bmi/about/index.html".toUri()
                    )
                    startActivity(intent)
                }
            }

        }
        updateBmi()
    }

    private fun updateBmi() {
        lifecycleScope.launch(Dispatchers.Default) {
            val height = CloudStore.get<UserGuide>(DOC_USER_GUIDE)?.heightCm ?: 0

            val weight = CloudStore.getListItem<LogWeightBean>(
                DOC_LOG_WEIGHT,
                DOC_LOG_WEIGHT,
                Filter.lessThanOrEqualTo("createTime", System.currentTimeMillis())
            )?.weightKg ?: 0
            val bmi = BmiUtils.getBmi(weight, height)
            val level = BmiUtils.getLevel(bmi)
            val data = BmiUtils.getData(level)
            withContext(Dispatchers.Main) {
                viewBind {
                    tvBmi.text = bmi.toString()
                    tvBmiLabel.backgroundTintList = ColorStateList.valueOf(data.second)
                    tvBmiLabel.text = data.first
                    sbBmi.progress = level * 2 + 1
                }
            }
        }
    }
}